//
//  AddGroupChartVC.m
//  PilotSeries
//
//  Created by 黄立强 on 16/7/4.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "AddGroupChartVC.h"
#import "PAGroupManager.h"
#import "ChartGroup.h"

@interface AddGroupChartVC ()

@end

@implementation AddGroupChartVC

- (void)viewDidLoad {
    [super viewDidLoad];
     [self initializeForm];
    self.navigationItem.rightBarButtonItem  = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(configGroup:)];
}



- (void)initializeForm {
    XLFormDescriptor * form;
    XLFormSectionDescriptor * section;
    XLFormRowDescriptor * row;
    form = [XLFormDescriptor formDescriptor];
    //    form.assignFirstResponderOnShow = YES;
    section = [XLFormSectionDescriptor formSectionWithTitle:@"Create Chart Group"];
    // Name
    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"groupName" rowType:XLFormRowDescriptorTypeText title:@"groupName"];
    [row.cellConfigAtConfigure setObject:@(NSTextAlignmentRight) forKey:@"textField.textAlignment"];
    
    if (self.operateType == PAGroupChartOperateTypeADD) {
        row.disabled = @NO;
        row.value =@"";
    }else{
        
        row.disabled = @YES;
        row.value =self.groupName;
    }
    
    row.required = YES;
    [section addFormRow:row];
    
    
    row = [XLFormRowDescriptor formRowDescriptorWithTag:@"chartConfigs" rowType:XLFormRowDescriptorTypeMultipleSelector title:@"chartNames"];
    NSArray *configs = [PAAppContext sharedInstance].chartNames;
    row.selectorOptions = (configs ==nil)?@[]:configs;
    if (self.operateType == PAGroupChartOperateTypeADD) {
        
    }else{
        ChartGroup *chartGroup = [PAGroupManager loadGroupByGroupName:self.groupName];
        row.value = chartGroup.chartConfigs;
    }
    
    row.required = YES;
    [section addFormRow:row];
    
    [form addFormSection:section];
    self.form = form;
}

- (void)configGroup:(UIBarButtonItem *)item{
    
    NSDictionary *dic = self.formValues;
    ChartGroup *chartGroup = [ChartGroup mj_objectWithKeyValues:dic];
    if (self.operateType == PAGroupChartOperateTypeADD) {
      //判断是否已经有相同的groupname,有则提示用户
       NSArray *groupNames =  [PAGroupManager loadGroupName];
        if ([groupNames containsObject:dic[@"groupName"]]) {
            [CRToastManager showNotificationWithMessage:@"this group already exists !" completionBlock:^{
                
            }];
            return;
        }
        [PAGroupManager updateGroup:chartGroup.groupName withChartNames:chartGroup.chartConfigs];
        [[NSNotificationCenter defaultCenter] postNotificationName:PAGroupChartADDNotification object:nil userInfo:@{@"chartGroup":chartGroup}];
        
    }else if(self.operateType == PAGroupChartOperateTypeUpdate){

        [PAGroupManager updateGroup:chartGroup.groupName withChartNames:chartGroup.chartConfigs];
        
      
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
}

@end
